import LayoutHeader2 from '@/components/LayoutHeader2';

import type { Settings as LayoutSettings } from '@ant-design/pro-components';
import type { RunTimeLayoutConfig } from '@umijs/max';
import defaultSettings from '../config/defaultSettings';
import { errorConfig } from './requestErrorConfig';

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 *  初始化某些全局数据的运行时配置
 * */
export async function getInitialState(): Promise<{
  isLogin: boolean; // 是否登录
  currentUser: API.CurrentUser | undefined; // 当前用户信息
  settings?: Partial<LayoutSettings>; // layout 配置
  loading?: boolean; // 全局loading
  // fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
  permissons?: any[]; // 用户权限
}> {
  //根据本地token判断是否登录
  const isLogin = true;
  //根据本地token获取用户信息
  const currentUser = JSON.parse(localStorage.getItem('currentUser') as string);

  history.push('/');
  return {
    isLogin,
    currentUser,
    settings: defaultSettings as Partial<LayoutSettings>,
  };
}

/**
 * @name layout 配置
 * onPageChange(locale) 有布局的页面切换时的回调 如果路由的layout: false,则不会触发onPageChange
 * onPageChange里可以使用useDispatch和useSelector来修改和获取dva的state
 * @doc https://procomponents.ant.design/components/layout
 */
export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  return {
    menuRender: false,
    headerRender: () => {
      return <LayoutHeader2 />;
    },
    // headerRender: false,
    logo: false,
    ...initialState?.settings,
    //布局页面切换时的回调
    onPageChange: () => {},
  };
};

/**
 * @param  location,routes,clientRoutes
 * onRouteChange 切换路由时触发 等于vue的全局路由守卫
 * onRouteChange、useEffect、onPageChange三者的先后顺序为：onRouteChange -> useEffect -> onPageChange
 * 注意(坑) onRouteChange里要使用dva的dispatch 需要在app.tsx里面引入dvaApp 无法直接使用useDispatch
 */
export const onRouteChange = ({}) => {};

/**
 * @name request 配置，可以配置错误处理
 * 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
 * @doc https://umijs.org/docs/max/request#配置
 */
export const request = {
  ...errorConfig,
};
